/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.web.formmetadata.replication.adjust;

import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.lcm.metadata.api.IMetadataContent;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.common.configuration.MetadataSerializerHelper;
import com.inspur.edp.lcm.metadata.spi.MetadataContentSerializer;
import com.inspur.edp.web.common.customexception.WebCustomException;
import com.inspur.edp.web.common.serialize.SerializeUtility;
import io.iec.edp.caf.commons.exception.ExceptionLevel;

/**
 * GspViewModel 的元数据拷贝 内容调整
 * 保证VO元数据，元数据描述的id和code与VO内容中数据的一致性
 *
 * @author guozhiqi
 */
class GspViewModelCloneAdjustStrategy implements GspMetadataCloneAdjustStrategy {
    @Override
    public void adjust(GspMetadata replicateMetadata, IMetadataContent replicateMetadataContent) {
        if (!(replicateMetadataContent instanceof GspViewModel)) {
            return;
        }
        GspViewModel replicateGSPViewModel = (GspViewModel) replicateMetadataContent;

        String oldViewModelCode = replicateGSPViewModel.getCode();

        String generatingAssemblyName = replicateGSPViewModel.getGeneratingAssembly().substring(0, replicateGSPViewModel.getGeneratingAssembly().lastIndexOf("."));

        replicateGSPViewModel.setGeneratingAssembly(replicateGSPViewModel.getGeneratingAssembly().replace(generatingAssemblyName, replicateMetadata.getHeader().getNameSpace()));

        replicateGSPViewModel.setID(replicateMetadata.getHeader().getId());

        replicateGSPViewModel.getVariables().setName(replicateGSPViewModel.getVariables().getName().replace(oldViewModelCode, replicateMetadata.getHeader().getCode()));
        replicateGSPViewModel.setName(replicateMetadata.getHeader().getName());

        replicateGSPViewModel.getVariables().setCode(replicateGSPViewModel.getVariables().getCode().replace(oldViewModelCode, replicateMetadata.getHeader().getCode()));
        replicateGSPViewModel.setCode(replicateMetadata.getHeader().getCode());

        MetadataContentSerializer transferSerializerManager = MetadataSerializerHelper.getInstance().getManager(replicateMetadata.getHeader().getType());
        if (transferSerializerManager == null) {
            throw new WebCustomException("未能正常获取元数据传输序列化器，请检查配置", ExceptionLevel.Error);
        }


        String replicateGSPViewModelStr = transferSerializerManager.Serialize(replicateGSPViewModel).toString();
        String i18nResourceInfoPrefix = replicateMetadata.getHeader().getNameSpace() + "." + "Vo" + ".";
        replicateGSPViewModelStr = replicateGSPViewModelStr.replace(i18nResourceInfoPrefix + oldViewModelCode, i18nResourceInfoPrefix + replicateGSPViewModel.getCode());

        replicateMetadata.setContent(transferSerializerManager.DeSerialize(SerializeUtility.getInstance().readTree(replicateGSPViewModelStr)));
    }
}
